package com.cooba.adapter;


import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.VideoBeans;
import com.cooba.utils.Constants;
import com.cooba.utils.DateUtils;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.qq.e.ads.banner.ADSize;
import com.qq.e.ads.banner.AbstractBannerADListener;
import com.qq.e.ads.banner.BannerView;

import java.util.List;

public class VideoAdapter extends BaseAdapter {
    private Activity context;
    private List<VideoBeans.VideoListEntity> list;

    public List<VideoBeans.VideoListEntity> getList() {
        return list;
    }

    public void setList(List<VideoBeans.VideoListEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public VideoAdapter(Activity context, List<VideoBeans.VideoListEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list==null){
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VideoViewHolder holder = null;
        if (convertView == null) {
            holder = new VideoViewHolder();
            convertView = LayoutInflater.from((context)).inflate(R.layout.video_list_item, parent, false);
            holder.img = (ImageView) convertView.findViewById(R.id.iv_video);
            holder.description = (TextView) convertView.findViewById(R.id.tv_video);
            holder.title = (TextView) convertView.findViewById(R.id.tv_videomsg);
            holder.videoTime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.bannerContainer);
            convertView.setTag(holder);
        } else {
            holder = (VideoViewHolder) convertView.getTag();
        }
        holder.title.setText(list.get(position).getView()+"次播放");
        int duration = list.get(position).getDuration(); //视频时长
        //时长秒转换为分钟
        String videoTime =  DateUtils.secToTime(duration);

        holder.videoTime.setText(videoTime);
        holder.description.setText(Utils.replaceString(list.get(position).getIntro()));
        String imgUrl = list.get(position).getImg_url();

        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.display(holder.img, imgUrl);

        BannerView bv = new BannerView(context, ADSize.BANNER, Constants.APPID, Constants.BannerPosID);
        if(position%5==0){
            bv.setRefresh(30);
            bv.setADListener(new AbstractBannerADListener() {
                @Override
                public void onNoAD(int arg0) {
                    Log.i("AD_DEMO", "BannerNoAD，eCode失败" + arg0);
                }

                @Override
                public void onADReceiv() {
                    Log.i("AD_DEMO", "ONBannerReceive成功");
                }
            });
            holder.relativeLayout.addView(bv);
            bv.loadAD();
            holder.relativeLayout.setVisibility(View.VISIBLE);
        }else {
            holder.relativeLayout.removeAllViews();
        }
         return convertView;
    }

    class VideoViewHolder {
        private ImageView img;
        public TextView title;
        public TextView description;
        public TextView videoTime;
        public RelativeLayout relativeLayout;
    }

}